<?php

namespace App\Admin\Controllers\OwnerItem;

use App\Admin\Repositories\OwnerItem;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class OwnerItemController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new OwnerItem(['owner', 'owner.ownerable', 'owner.user']), function (Grid $grid) {
            $grid->id->sortable();
            $grid->column('owner.user', '业主姓名')->display(function ($value) {
                return $value['name'];
            });
            $grid->column('owner.ownerable', '关联的房间')->display(function ($value) {
                return $value['name'];
            });
            $grid->count('第几期')->display(function ($value) {
                return "第{$value}期租金";
            });
            $grid->pay_at('支付时间')->display(function ($value) {
                return $value ? $this->total_amount : "未支付";
            });
            $grid->rent('本期租金')->display(function ($value) {
                return "本期租金：" . $value;
            });
            $grid->other_amount('杂费')->display(function ($value) {
                return $value ? $value : "未填写";
            });
            $grid->status('支付状态')->display(function ($value) {
                return \App\Models\OwnerItem::$statusMap[$value];
            });
            $grid->total_amount('总金额')->display(function ($value) {
                return $value ? $value : "未填写";
            });
            $grid->started_at->sortable();
            $grid->ended_at;

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new OwnerItem(), function (Show $show) {
            $show->id;
            $show->owner_id;
            $show->count;
            $show->pay_at;
            $show->rent;
            $show->other_amount;
            $show->status;
            $show->total_amount;
            $show->started_at;
            $show->ended_at;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new OwnerItem(), function (Form $form) {
            $form->display('id');
            $form->text('owner_id');
            $form->text('count');
            $form->text('pay_at');
            $form->text('rent');
            $form->text('other_amount');
            $form->text('status');
            $form->text('total_amount');
            $form->text('started_at');
            $form->text('ended_at');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
